#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :weibo_数据保存本地版.py
# @Time      :2024/9/9 
# @Author    :CL
# @email     :1037654919@qq.com
import json
from datetime import datetime
import requests
from weibo_comment import get_comment
from weibo_uinfo import get_uinfo
def get_mymblog(page = 1):
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "client-version": "v2.46.18",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://weibo.com/u/2047010597",
        "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "server-version": "v2024.09.26.1",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
        "x-xsrf-token": "Mc2Cr-5qSBSOo4eD1HX4Nvu2"
    }
    cookies = {
        "SCF": "AiHhHpjpUWHWnw4SPlbn0Blpe01ucnX2MUzKWh0fqSb932tmszUvmR2tEDRwpz38lWYfoQpXJIu8xqXQbrL_X-o.",
        "XSRF-TOKEN": "Mc2Cr-5qSBSOo4eD1HX4Nvu2",
        "_s_tentry": "weibo.com",
        "Apache": "8051481509490.477.1725417483785",
        "SINAGLOBAL": "8051481509490.477.1725417483785",
        "ULV": "1725417483789:1:1:1:8051481509490.477.1725417483785:",
        "SUBP": "0033WrSXqPxfM725Ws9jqgMF55529P9D9WhmxpO6_7Bbx4r5qcvFEF1M5JpX5KMhUgL.Fo-Neo501K5RS0.2dJLoI0qLxK-LBo5L12qLxK-LBo2L1h2LxK-LB-BLBK.LxKML1-2L1hBLxK-L1K5L12eLxKqLBK2L12et",
        "ALF": "1730854541",
        "SUB": "_2A25KB0HdDeRhGeNJ6VIS-S7EzDWIHXVpfdsVrDV8PUJbkNAbLWb2kW1NS_5D5SPwbucHHx-l-0NAEc1L7w8uBvJA",
        "WBPSESS": "G9Ff9BQjWfN2Zb8Vj7-gc-yXFs3Y9sWM72U5flc0bOD_g3vusEwwgCVT1Zq8J4AH2elxXtgHZJ1I_h31Mzpt_9y8nsw6vXT6hZRygZ1DbBkrDjnSi0kiAqZrHEdNpSL2qy6eM-CvVYML3_H3cxRUPg=="
    }
    url = "https://weibo.com/ajax/statuses/mymblog"
    params = {
        "uid": "3919603060",
        "page":page,
        "feature": "0"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    print(response.url,response)

    # print(response.text)
    return response.json()['data']['list']

def save_uinfo():
    uinfo = get_uinfo()
    user = {}
    user['id'] = uinfo['id']
    user['screen_name'] = uinfo['screen_name']
    user['转评赞'] = uinfo['status_total_counter']['total_cnt']
    user['累计评论'] = uinfo['status_total_counter']['comment_cnt']
    user['累计转发'] = uinfo['status_total_counter']['repost_cnt']
    user['累计获赞'] = uinfo['status_total_counter']['like_cnt']
    user['认证'] = uinfo['verified_reason']
    user['描述'] = uinfo['description']
    user['粉丝量'] = uinfo['followers_count']
    user['关注量'] = uinfo['friends_count']
    timestamp_seconds = int(datetime.now().timestamp())
    user["_id"] = f"{uinfo['id']}_{timestamp_seconds}"
    user['时间戳'] = timestamp_seconds
    # 格式化当前时间为标准形式
    now = datetime.now()
    current_time = now.strftime("%Y%m%d %H:%M:%S")

    user['时间'] =current_time
    return user
def save_myblog():
    for page in range(1,3):
        mymblog = get_mymblog(page=page)
        for blog in mymblog:
            reslut = {}
            reslut['_id'] = blog['id']  # created_at
            reslut['uid'] = blog['user']['id']
            # 原始时间字符串
            time_str = blog['created_at']
            # 解析时间字符串
            dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
            # 格式化为所需格式
            formatted_time = dt.strftime("%Y%m%d %H:%M")
            reslut['time'] = formatted_time
            reslut['text_raw'] = blog['text_raw'] if blog['text_raw'] != '转发微博' else '转发微博：' + blog['retweeted_status'][
                'text_raw']
            yield  reslut

def save_comments(reslut):
    max_id =0
    params = {
        "flow": "1",  # 按时间
        "is_reload": "1",
        "id": reslut['_id'],
        "is_show_bulletin": "2",
        "is_mix": "0",
        "count": "20",
        'max_id': max_id,
        "uid": reslut['uid'],
        "fetch_level": "0",
        "locale": "zh"
    }
    while True:
        comments = get_comment(params=params)
        params['max_id'] = comments['max_id'] if 'max_id' in comments else 0
        print (reslut['_id'],params['max_id'])
        if params['max_id'] == 0:
            break
        for comment in comments['data']:
            for comment2 in comment['comments']: # 保存二级评论
                reslut2 = {}
                reslut2["_id"] = comment2['id']
                # if weibo_comments.findOne({'_id': comment2['id']}):
                #     continue
                reslut2["评论type"] ='二级评论'
                reslut2['微博id'] = reslut['_id']
                reslut2["微博原文"] = reslut['text_raw']
                reslut2["一级评论文本"] =  comment['text_raw']
                reslut2["来源"] = comment2['source']
                reslut2["评论用户"] = comment2['user']['screen_name']
                time_str = comment2['created_at']
                # 解析时间字符串
                dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
                # 格式化为所需格式
                formatted_time = dt.strftime("%Y%m%d %H:%M")
                reslut2['time'] = formatted_time
                reslut2["评论序号"] = comment2['floor_number']
                reslut2["二级评论文本"] = comment2['text_raw']
                reslut2["点赞量"] = comment2['like_counts']
                yield reslut2
            reslut1 = {}
            reslut1["_id"] = comment['id']  #
            # if weibo_comments.findOne({"_id": comment['id']}):
            #     continue
            reslut1["评论type"] = '一级评论'
            reslut1['微博id'] = reslut['_id']
            reslut1["微博原文"] = reslut['text_raw']
            reslut1["一级评论文本"] = comment['text_raw']
            reslut1["来源"] = comment['source']
            reslut1["评论用户"] = comment['user']['screen_name']
            time_str = comment['created_at']
            # 解析时间字符串
            dt = datetime.strptime(time_str, "%a %b %d %H:%M:%S %z %Y")
            # 格式化为所需格式
            formatted_time = dt.strftime("%Y%m%d %H:%M")
            reslut1['time'] = formatted_time
            reslut1["评论序号"] = comment['floor_number']
            reslut1["点赞量"] = comment['like_counts']
            reslut1["回复量"] = comment['total_number']
            yield reslut1

def save_main():

    myblog =[]
    commentss1=[]
    commentss2= []
    for seed in save_myblog():
        myblog.append(seed)
        for comment in save_comments(seed):
            if comment["评论type"] =='二级评论':
                commentss2.append(comment)
            else:
                commentss1.append(comment)

    import  pandas as pd

    # 定义列名
    columns1 = [
        "_id", "评论type", "微博id", "微博原文", "一级评论文本", "来源", "评论用户", "time", "评论序号",
        "点赞量", "回复量"
    ]
    columns2 = [
        "_id", "评论type", "微博id", "微博原文", "一级评论文本", "来源", "评论用户", "time", "评论序号",
        "二级评论文本", "点赞量"
    ]
    pd.DataFrame(commentss1,columns=columns1).to_excel('一级评论.xlsx', index=False)
    pd.DataFrame(commentss2,columns=columns2).to_excel('二级评论.xlsx', index=False)

if __name__ == "__main__":
    print()
    save_main()
